package com.company;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.io.IOException;
import java.net.URL;

public class Main {

    public static void main(String[] args) throws IOException {
	// write your code here
        HttpURLConnection connection = null;
        InputStream is = null;
        try{
            //定义网址链接地址
            String myUrl = "http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html";
            URL url = new URL(myUrl);

            connection = (HttpURLConnection) url.openConnection();
            int code = connection.getResponseCode();
            //响应码等于200表示成功
            if(code == 200){
                is = connection.getInputStream();

                BufferedReader br = new BufferedReader(new InputStreamReader((is)));

                String line = "http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html";
                StringBuffer sb = new StringBuffer();

                while ((line = br.readLine()) != null){
                    sb.append(line);
                }
                String result = sb.toString();
                System.out.println(result);

                //
                String path = "/Users/lanouhn/Desktop/wangyi.txt";
                File file = new File(path);

                FileWriter fw = new FileWriter(file);
                BufferedWriter writer = new BufferedWriter(fw);
                writer.write(result);
            }

        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(connection != null){
                connection.disconnect();
            }
            if (is != null){
                is.close();
            }
        }
    }
}
